from shutil import copyfile
from video_compressor import THREAD_POOL
from video_compressor import printf
from video_compressor import Colors
from video_compressor import VIDEO_WIDE
from video_compressor import VIDEO_HIGH
from video_compressor import IN_DIR_PATH
from video_compressor import OUT_DIR_PATH
from video_compressor import VIDEO_EXTS
from video_compressor import get_file
from video_compressor.utils import get_video_info
from video_compressor.ffmpeg_video import ffmpeg_compressor
from video_compressor.ffmpeg_video import run


def get_tasks():
    tasks = []
    file_iter = get_file(IN_DIR_PATH)
    for file in file_iter:
        out_dir_path = OUT_DIR_PATH.joinpath(file.relative_to(IN_DIR_PATH))
        if not out_dir_path.parent.is_dir(): out_dir_path.parent.mkdir(parents=True, exist_ok=True)
        if out_dir_path.suffix not in VIDEO_EXTS:
            copyfile(file, out_dir_path)
        else:
            if out_dir_path.is_file(): continue
            wide, high, total_time = get_video_info(file)
            video_wide = VIDEO_WIDE if VIDEO_WIDE else wide
            video_high = VIDEO_HIGH if VIDEO_HIGH else high
            printf(f'{Colors.OKGREEN}创建任务{Colors.ENDC}{Colors.OKBLUE}{out_dir_path}{Colors.ENDC}')
            ffmpeg = ffmpeg_compressor(file, out_dir_path, video_wide, video_high)
            tasks.append(THREAD_POOL.submit(run, ffmpeg, out_dir_path))
    return tasks